def set_stage():
""" Sets up the stage for the game """
stage.set_background("soccerfield")
stage.disable_floor()
def add_player():
""" Adds a player to the stage for the user to control """
player = codesters.Sprite("player1")
player.go_to(0, -155)
return player
def add_ball():
""" Adds a ball to the stage and sets its attributes """
ball = codesters.Sprite("soccerball")
ball.set_y_speed(-8)
def head_ball(sprite, hit_sprite):
""" Detects collisions between the player and ball """
my_var = hit_sprite.get_y_speed()
hit_sprite.set_y_speed(-my_var + 1)
my_var = hit_sprite.get_x_speed()
hit_sprite.set_x_speed(my_var + 1)
def move_left():
""" Moves the player left """
player.move_left(50)
def move_right():
""" Moves the player right """
player.move_right(50)
def main():
""" Sets up the program and calls other functions """
set_stage()
player = add_player()
add_ball()
player.event_collision(head_ball)
stage.event_key("left", move_left)
stage.event_key("right", move_right)
main()
t = codesters.Teacher()
defs = t.find_block("def")
add_balls = t.find_text("add_ball")
num_add_balls = len(add_balls)
set_y_speeds = t.find_text("set_y_speed")
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
try:
tval1 = add_balls[-1][1].replace(' ','').lower()
tval1_line_num = add_balls[-1][0]
tval1_indent = t.get_indent_at_line(tval1_line_num)
except:
tval1 = "DNE"
tval1_line_num = "DNE"
tval1_indent = "DNE"
try:
tval1_def, tval1_distance = get_above_def(tval1_line_num, defs)
except:
tval1_def = "DNE"
tval1_distance = "DNE"
try:
tval2 = defs[2][1].replace(' ','').lower()
except:
tval2 = "DNE"
try:
tval3 = set_y_speeds[0][1].replace(' ','').lower()
except:
tval3 = "DNE"
try:
tval4 = add_balls[-2][1].replace(' ','').lower()
except:
tval4 = "DNE"
t1 = TestObjective()
t1.add_success(num_add_balls >= 3, "Great job!")
t1.add_failure(num_add_balls < 3, "Did you add another add_ball() function call in the main() function?")
t2 = TestObjective()
t2.add_success("main" in tval1_def and tval1_indent == 4 and num_add_balls >= 3, "Great job!")
t2.add_failure(tval1_def == "DNE" or tval1_indent == "DNE", "Did you add another add_ball() function call in the main() function?")
t2.add_failure("main" not in tval1_def and tval1_indent == 4, "Did you place the Function Call inside the main() function?")
t2.add_failure(tval1_indent < 4 or tval1_indent > 4, "Oops, make sure the add_ball() function call is indented once within main()!")
t2.add_failure(num_add_balls < 3, "Did you add another add_ball() function call in the main() function?")
t3 = TestObjective()
t3.add_success("speed" in tval2, "Great job!")
t3.add_failure(tval2 == "DNE", "Oops, did you delete a function?")
t3.add_failure("speed" not in tval2, "Did you add speed as a parameter in the add_ball() function definition?")
t4 = TestObjective()
t4.add_success("(speed)" in tval3, "Great job!")
t4.add_failure(tval3 == "DNE", "Did you delete a set_y_speed() command?")
t4.add_failure("(-8)" in tval3, "Did you replace -8 with speed in set_y_speed()?")
t5 = TestObjective()
t5.add_success("()" not in tval1 and "()" not in tval4, "Great job!")
t5.add_failure("()" in tval1 or "()" in tval4, "Make sure you add an integer parameter to both add_ball() calls in main()!")
tester = TestManager()
tester.add_test_list([t1, t2, t3, t4, t5])
tester.run_tests()
tester.display_first_feedback()
-
Run Code
-
Activity Submitted!
Enviar Trabajo
-
Actividad Siguiente
-
Stop Running Code
-
Show Chart
-
Show Console
-
Reset Code Editor
-
Codesters How To (opens in a new tab)